//
//  AppDelegate.m
//  DistributionMallApp
//
//  Created by JHnoplan on 2018/10/18.
//  Copyright © 2018年 JHnoplan. All rights reserved.
//

#import "AppDelegate.h"
#import "DMRootVC.h"
#import <YTKNetwork/YTKNetwork.h>
#import "KBaseAPI.h"
#import <AlipaySDK/AlipaySDK.h>
#import <WXApi.h>
#import "WXApiManager.h"
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    [[YTKNetworkConfig sharedConfig]setBaseUrl:KBaseURL];
    
    self.window=[[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = DMWhiteColor;
    self.window.rootViewController=[DMRootVC chooseWindowRootVC];
    [self.window makeKeyAndVisible];
    
#pragma mark - 启动自动键盘处理,点击屏幕收回
    [IQKeyboardManager sharedManager].enable = YES;
    [IQKeyboardManager sharedManager].shouldResignOnTouchOutside = YES;
    //注册微信
    [WXApi registerApp:@"wx664269d80a61ae7f"];
    
    return YES;
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

#pragma mark --- 第三方回调 ---

-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    if ([url.host isEqualToString:@"safepay"]) {
        //跳转支付宝钱包进行支付，处理支付结果
        [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {

            [[NSNotificationCenter defaultCenter]postNotificationName:KAliPayNotiName object:resultDic];
            
        }];
    }else if ([url.host isEqualToString:@"pay"]){
        //跳转微信进行支付，处理支付结果
        return [WXApi handleOpenURL:url delegate:[WXApiManager sharedManager]];
        
    }
    
    return YES;
}

-(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options{
    
    
    if ([url.host isEqualToString:@"safepay"]) {
        //跳转支付宝钱包进行支付，处理支付结果
        [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {

            [[NSNotificationCenter defaultCenter]postNotificationName:KAliPayNotiName object:resultDic];
        }];
    }else if ([url.host isEqualToString:@"pay"]){
        //跳转微信进行支付，处理支付结果
        return [WXApi handleOpenURL:url delegate:[WXApiManager sharedManager]];
        
    }
    
    return YES;
    
}

-(BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
    if ([url.host isEqualToString:@"safepay"]) {
        //跳转支付宝钱包进行支付，处理支付结果
        [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {

            [[NSNotificationCenter defaultCenter]postNotificationName:KAliPayNotiName object:resultDic];
            
        }];
    }else if ([url.host isEqualToString:@"pay"]){
        //跳转微信进行支付，处理支付结果
        return [WXApi handleOpenURL:url delegate:[WXApiManager sharedManager]];
        
    }
    
    return YES;
}

@end
